package org.bluedream.comm.utils;

import java.util.Random;

import static org.apache.commons.lang3.StringUtils.replace;
import static org.apache.commons.lang3.StringUtils.trim;

public class StringUtils {
    //Suppress default constructor for noninstantiability
    private StringUtils(){
        throw new AssertionError();
    }

    public static String getTreeNodeName(String isShowCode, String code, String name) {
        if (!"true".equals(isShowCode) && !"1".equals(isShowCode)) {
            return "2".equals(isShowCode) ? replace(name, " ", "") + "[" + code + "]" : replace(name, " ", "");
        } else {
            return "[" + code + "]" + replace(name, " ", "");
        }
    }

    public static String getRandomString(int arg0){
        String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        for(int i = 0;i < arg0;i++){
            int number=random.nextInt(62);
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    public static byte uniteBytes(String src0, String src1) {
        byte b0 = Byte.decode("0x" + src0);
        b0 = (byte)(b0 << 4);
        byte b1 = Byte.decode("0x" + src1);
        byte ret = (byte)(b0 | b1);
        return ret;
    }

    /**
     * 字符串（16进制）转byte数组
     * @param src
     * @return
     */
    public static byte[] hexStr2Bytes(String src) {
//        int m = false;
//        int n = false;
        int l = src.length() / 2;
        byte[] ret = new byte[l];
        for(int i = 0; i < l; ++i) {
            int m = i * 2 + 1;
            int n = m + 1;
            ret[i] = StringUtils.uniteBytes(src.substring(i * 2, m), src.substring(m, n));
        }

        return ret;
    }

    /**
     * byte数组转字符串（16进制）
     * @param data
     * @param digits
     * @return
     */
    public static String toHex(byte[] data , String digits) {
        return toHex(data, data.length , digits);
    }

    /**
     * byte数组转字符串（16进制）
     * @param data
     * @param length
     * @param digits
     * @return
     */
    public static String toHex(byte[] data, int length , String digits) {
        StringBuffer buf = new StringBuffer();

        for(int i = 0; i != length; ++i) {
            int v = data[i] & 255;
            buf.append(digits.charAt(v >> 4));
            buf.append(digits.charAt(v & 15));
        }
        return buf.toString();
    }

    /**
     * byte数组转HEX字符串
     * @param b
     * @return
     */
    public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";

        for(int n = 0; n < b.length; ++n) {
            stmp = Integer.toHexString(b[n] & 255);
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }

        return hs.toUpperCase();
    }



    public static byte[] hex2byte(String s) {
        int l = s.length() / 2;
        byte[] b = new byte[l];

        for(int i = 0; i < l; ++i) {
            int m = i * 2 + 1;
            int n = m + 1;
            b[i] = StringUtils.uniteBytes(s.substring(i * 2, m), s.substring(m, n));
        }
        return b;
    }

    public static boolean inStringIgnoreCase(String str, String... strs) {
        if (str != null && strs != null) {
            String[] var2 = strs;
            int var3 = strs.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                String s = var2[var4];
                if (str.equalsIgnoreCase(trim(s))) {
                    return true;
                }
            }
        }

        return false;
    }
}
